﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Kxx_Log.Log
{
    public class DNSLog
    {
        private static int logClearMaxCount = 1000;  //日志清理保留条数
        private static int logClearTime = 12 * 60 * 60 * 1000;  //日志清理时间间隔（12小时）
        private static ObservableCollection<LogModel> logList = new ObservableCollection<LogModel>();
        public static ObservableCollection<LogModel> LogList
        {
            get { return logList; }
        }

        public static void LogAdd(LogType type, string logSource, string logInfo)
        {
            LogModel model = new LogModel();
            model.Type = type;
            model.LogSource = logSource;
            model.LogInfo = logInfo;

            LogList.Add(model);
        }

        /// <summary>
        /// 日志清理任务
        /// </summary>
        public static void LogClearTask()
        {
            new Task(() =>
            {
                while (true)
                {
                    while (logList.Count > logClearMaxCount)
                    {
                        logList.RemoveAt(0);
                    }

                    Thread.Sleep(logClearTime);
                }
            }).Start();
        }
    }

    public enum LogType
    {
        Info,
        Success,
        Warning,
        Error
    }

    public class LogModel
    {
        /// <summary>
        /// 日志类别
        /// </summary>
        private LogType type;
        public LogType Type
        {
            get { return type; }
            set { type = value; }
        }

        /// <summary>
        /// 日志来源
        /// </summary>
        private string logSource;
        public string LogSource
        {
            get { return logSource; }
            set { logSource = value; }
        }

        /// <summary>
        /// 日志时间
        /// </summary>
        private DateTime logTime = DateTime.Now;
        public DateTime LogTime
        {
            get { return logTime; }
            set { logTime = value; }
        }

        /// <summary>
        /// 日志信息
        /// </summary>
        private string logInfo;
        public string LogInfo
        {
            get { return logInfo; }
            set { logInfo = value; }
        }
    }
}
